package cn.quevo.ucenter.server.configuration;

import javax.sql.DataSource;

import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.DefaultNameConversion;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import cn.quevo.beetl.spring.boot.autoconfigure.BeetlMapperScannerRegister;
import cn.quevo.beetl.spring.boot.autoconfigure.BeetlSqlProperties;
import cn.quevo.beetlsql.BeetlBeanProcessor;
import cn.quevo.beetlsql.BeetlSQLFactory;

@Configuration
@ConditionalOnClass(DataSource.class)
@Import(BeetlMapperScannerRegister.class)
public class BeetlSqlConfiguration {
	private static final Logger log = LoggerFactory.getLogger(BeetlSqlConfiguration.class);
	
	@Bean(name="beetlSqlDataSource")
	public BeetlSqlDataSource beetlSqlDataSource(@Qualifier("dataSource")  DataSource dataSource/*,@Qualifier("slave")  DataSource slave*/){
	    BeetlSqlDataSource source = new BeetlSqlDataSource();
	    source.setMasterSource(dataSource);
	    //source.setSlaves(new DataSource[]{slave});
	    return source;
	}
	
	@Bean(name = "sqlManagerFactoryBean")
	@Primary
	public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("beetlSqlDataSource") BeetlSqlDataSource source,Environment env) {
		BeetlSqlProperties beetlSqlProperties = new BeetlSqlProperties(env);
		SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
		factory.setCs(source);
		factory.setDbStyle(beetlSqlProperties.getDBStyleInstance());
		if(beetlSqlProperties.isDebug()) {
			factory.setInterceptors(new Interceptor[] { new DebugInterceptor()});
		}
		factory.setNc(new DefaultNameConversion());
		factory.setSqlLoader(new ClasspathLoader(beetlSqlProperties.getSqlPath()));
		try {
			factory.getObject().setDefaultBeanProcessors(new BeetlBeanProcessor(factory.getObject()));
		} catch (Exception e) {
			log.error("初始化BeetlSql异常", e);
		}
		return factory;
	}
	
	@Bean("beetlSqlFactory")
	public BeetlSQLFactory getBeetlSQLFactory(
			@Qualifier("sqlManagerFactoryBean") SqlManagerFactoryBean sqlManagerFactoryBean) {
		return new BeetlSQLFactory(sqlManagerFactoryBean);
	}
	
	@Bean(name = "transactionManager")
	public DataSourceTransactionManager getDataSourceTransactionManager(
			@Qualifier("dataSource") DataSource datasource) {
		DataSourceTransactionManager dsm = new DataSourceTransactionManager();
		dsm.setDataSource(datasource);
		return dsm;
	}
	
}
